package net.sxnic.item.print.word;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import net.sxnic.comm.basecode.BaseCodeUtils;
import net.sxnic.comm.utils.WordUtils;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.item.zzdw.Zzdw;
import net.sxnic.item.zzdw.util.ZzdwUtils;
import net.sxnic.jhitem.itemannex.ItemAnnex;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemssmb.ItemSsmb;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImportFormatMode;

/**
 * 项目基本信息
 * @author CGD
 *
 */
public class KjcxInfo {
	static {
		Document.setLicence("sj_laokai");
	}
	

	public static void createWord(String saveDocFile, String templateFile,
			Mdata mdata,ItemFund fund,ItemPrincipal principal,ItemClob clob,List<ItemPeople> peoples,List<ItemSsmb> ssmbs, List<ItemAnnex> annex002List,String basePath) {
		Document doc;
		try {
			File file = new File(saveDocFile);
			if (!file.getParentFile().exists()
					&& !file.getParentFile().mkdirs()) {
				return;
			}

			doc = new Document(templateFile);
			DocumentBuilder builder = new DocumentBuilder(doc);
			SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			// 设置标签
			WordUtils.setBookmarks(doc, mdata);
			WordUtils.setBookmarks(doc, clob);
			WordUtils.setBookmarks(doc, fund);
			//设置项目类别
			WordUtils.setBookmark(doc, "unitType",
					BaseCodeUtils.getInfoName("012", mdata.getUnitType()));
			
			if(StringUtils.isNotBlank(mdata.getZzdwCode())){
			WordUtils.setBookmark(doc, "zzdwCode",
					ItemConstants.ZZDW_MAP.get(ItemConstants.SYSTEM_CALALOG_ALIAS).get(mdata.getZzdwCode()));
			}
		
			//文档合并
			if(annex002List!=null&&annex002List.size()>0)
			{
				for(ItemAnnex itemAnnex : annex002List)
				{
					doc.appendDocument(new Document(itemAnnex.getFilePath()+itemAnnex.getAttachName()), ImportFormatMode.KEEP_SOURCE_FORMATTING);
				}

			}
			doc.save(saveDocFile);
		} catch (Exception ex) {
			ex.printStackTrace();
			try {
				doc = new Document(templateFile);
				doc.save(saveDocFile);
			} catch (Exception e) {
				e.fillInStackTrace();
			}
		}
	}

	}
